﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.Web.Security;
using System.Security.Cryptography;
using System.Text;
using Microsoft.Win32;
using System.Web.UI.WebControls;

namespace WebComponents.Security
{
    public static class DataCrypt
    {
        private static string myKey = "1.0.1";
        private static TripleDESCryptoServiceProvider cryptDES3 = new TripleDESCryptoServiceProvider();
        private static MD5CryptoServiceProvider cryptMD5Hash = new MD5CryptoServiceProvider();

        public static string Encrypt(string myString)
        {
            cryptDES3.Key = cryptMD5Hash.ComputeHash(ASCIIEncoding.ASCII.GetBytes(myKey));
            cryptDES3.Mode = CipherMode.ECB;
            ICryptoTransform desdencrypt = cryptDES3.CreateEncryptor();
            byte[] buff = ASCIIEncoding.ASCII.GetBytes(myString);
            return Convert.ToBase64String(desdencrypt.TransformFinalBlock(buff, 0, buff.Length)).Replace("+",".");
        }

        public static string Decrypt(string myString)
        {
            myString = myString.Replace(".", "+");
            cryptDES3.Key = cryptMD5Hash.ComputeHash(ASCIIEncoding.ASCII.GetBytes(myKey));
            cryptDES3.Mode = CipherMode.ECB;
            ICryptoTransform desdencrypt = cryptDES3.CreateDecryptor();
            byte[] buff = Convert.FromBase64String(myString);
            return ASCIIEncoding.ASCII.GetString(desdencrypt.TransformFinalBlock(buff, 0, buff.Length));
        }

        public static Label EncryptMe(this Label lbl)
        {
            string ec = Encrypt(lbl.Text);
            lbl.Attributes.Add("onclick", String.Format("JsText('{0}','{1}');", lbl.ClientID, ec));
            lbl.Text = "You are using text encrypt...";
            return lbl;
        }
    }
}